<template>
  <div class="zh-circle-group">
    <nav-header></nav-header>
    <div class="zh-table">
      <div class="zh-table-open clear">
        <div class="zh-open-left fl">
          <!-- 单级-显示学段-年级-科目筛选selcet框-->
          <template v-if="!platformInformation">
            <div class="open-width" style="width: 100px">
              <el-select
                v-model="listParam.study_section_id"
                @change="getGrade"
                filterable
                placeholder="请选择"
              >
                <el-option
                  v-for="item in orgLevel2"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </div>
            <div class="open-width" style="width: 100px">
              <el-select
                v-model="listParam.grade_id"
                @change="getSubjectList"
                filterable
                placeholder="请选择"
              >
                <el-option
                  v-for="item in orgLevel3"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </div>
            <div class="open-width" style="width: 100px">
              <el-select
                v-model="listParam.subject_id"
                filterable
                placeholder="请选择"
              >
                <el-option
                  v-for="item in orgSubject"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </div>
          </template>

          <div class="open-width">
            <el-input
              v-model="listParam.keyword"
              placeholder="请输入圈组名、创建人搜索"
              @keyup.enter.native="searchFunc"
            ></el-input>
          </div>
          <el-button type="primary" @click="searchFunc">搜索</el-button>
          <el-button type="primary" @click="reset">重置</el-button>
        </div>
        <div class="zh-open-right fr">
          <el-button type="primary" @click="newGroup">添加圈组</el-button>
        </div>
      </div>
      <div
        class="zh-table-contents"
        v-loading="loading.status"
        :element-loading-text="loading.txt"
      >
        <el-table :data="listData" border max-height="600" style="width: 100%">
          <el-table-column
            type="index"
            :index="typeIndex"
            label="序号"
            align="center"
            show-overflow-tooltip
            width="50"
          >
          </el-table-column>
          <el-table-column
            prop="name"
            label="圈组名称"
            align="center"
            show-overflow-tooltip
          >
          </el-table-column>

          <!-- 单级-显示学段-年级-科目-->
          <template v-if="!platformInformation">
            <el-table-column
              prop="study_name"
              label="学段"
              align="center"
              show-overflow-tooltip
            >
            </el-table-column>
            <el-table-column
              prop="grade_name"
              label="年级"
              align="center"
              show-overflow-tooltip
            >
            </el-table-column>
            <el-table-column
              prop="subject_name"
              label="学科"
              align="center"
              show-overflow-tooltip
            >
            </el-table-column>
          </template>

          <el-table-column
            prop="number"
            label="创建人帐号"
            align="center"
            show-overflow-tooltip
            width="150"
          >
          </el-table-column>
          <el-table-column
            prop="create_name"
            label="创建用户名"
            align="center"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column
            prop="create_time"
            label="创建时间"
            align="center"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column
            prop="u_count"
            label="圈组人数"
            align="center"
            show-overflow-tooltip
            width="80"
          >
          </el-table-column>
          <el-table-column
            prop="res_count"
            label="教案数"
            align="center"
            show-overflow-tooltip
            width="80"
          >
            <template slot-scope="scope">
              {{
                scope.row.res_count +
                scope.row.test_count +
                scope.row.video_count
              }}
            </template>
          </el-table-column>
          <el-table-column
            prop="news_count"
            label="公告数"
            align="center"
            show-overflow-tooltip
            width="80"
          >
          </el-table-column>
          <el-table-column
            prop="4"
            label="状态"
            align="center"
            show-overflow-tooltip
            width="80"
          >
            <template slot-scope="scope">
              <div v-html="getStatus(scope.row.status)"></div>
            </template>
          </el-table-column>
          <el-table-column
            prop="5"
            label="操作"
            align="center"
            width="154"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <template v-if="parseInt(scope.row.status) === 1">
                <!-- 教育局-成员查看 -->
                <el-button
                  v-if="platformInformation"
                  @click="review(scope.row.id)"
                  type="text"
                  >成员查看</el-button
                >
                <!-- 学校-成员审核 -->
                <el-button v-else @click="review(scope.row.id)" type="text"
                  >成员审核</el-button
                >

                <el-button @click="management(scope.row.id)" type="text"
                  >管理</el-button
                >
                <el-button @click="closesal(scope.row)" type="text"
                  >关闭</el-button
                >
              </template>
              <template v-if="parseInt(scope.row.status) === 2">
                <el-button
                  @click.stop="approve_confirm(scope.row)"
                  type="text"
                  size="small"
                  >通过</el-button
                >
                <el-button
                  @click.stop="refusal(scope.row)"
                  type="text"
                  size="small"
                  >拒绝</el-button
                >
              </template>
              <template v-if="parseInt(scope.row.status) === 3">
                <el-button
                  @click="delete_confirm(scope.row)"
                  type="text"
                  class="delete-button"
                  size="small"
                  >删除
                </el-button>
              </template>
              <template v-if="parseInt(scope.row.status) === 4">
                <el-button
                  @click="open_confirm(scope.row)"
                  type="text"
                  size="small"
                  >开启</el-button
                >
                <el-button
                  @click="delete_confirm(scope.row)"
                  type="text"
                  class="delete-button"
                  size="small"
                  >删除
                </el-button>
              </template>
            </template>
          </el-table-column>
        </el-table>
        <div class="table-pagination" v-show="total_number > 10">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="listParam.page_index"
            :page-sizes="[10, 20, 30, 40, 50, 100]"
            :page-size="listParam.page_size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total_number"
          >
          </el-pagination>
          <div class="pagination-button">
            <el-button
              @click="getList"
              type="primary"
              icon="el-icon-refresh"
              circle
              title="刷新数据"
              size="mini"
            >
            </el-button>
          </div>
        </div>
      </div>
    </div>
    <el-dialog
      title="确定关闭圈组吗？"
      :visible.sync="refusalVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      width="460px"
      custom-class="seeTxT"
    >
      <!-- <div>
					<el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
						<el-form-item prop="desc"  label-width="0">
						 	 <el-input type="textarea" 
						 	 rows="4" resize="none"
						 	 :maxlength="parseInt(200)"
						 	 placeholder="请输入拒绝理由, 不超200个字符"
						 	 v-model="ruleForm.desc"></el-input>
						 </el-form-item>
					</el-form>
				</div> -->
      <div slot="footer" class="dialog-footer">
        <el-button @click="refusalVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      loading: {
        status: false,
        txt: "获取数据...",
      },
      listData: [],
      listParam: {
        study_section_id: null,
        grade_id: null,
        subject_id: null,
        keyword: "",
        page_index: 1,
        page_size: 10,
      },
      orgLevel2Param: {
        //获取学段参数
        level: 2,
        pid: null,
        direction: "balance",
      },
      orgLevel3Param: {
        //获取年级参数
        level: 3,
        pid: null,
        direction: "balance",
      },
      orgLevel2: [{ id: null, name: "全部学段" }],
      orgLevel3: [{ id: null, name: "全部年级" }],
      orgSubject: [{ id: null, name: "全部科目" }],
      subjectParam: {
        //获取科目,版本，参数
        type: 2,
        org_id: null,
        status: 1,
      },
      total_number: 0,
      statusArr: { 1: "已通过", 2: "未审核", 3: "未通过", 4: "已关闭" }, //1：正常；0：已删除；2：未审核；3：未通过；4:关闭
      refusalVisible: false,
      ruleForm: {
        desc: "",
      },
      rules: {
        desc: [
          { validator: this.validateContent, trigger: "blur", required: true },
        ],
      },
    };
  },
  created() {
    this.getOrgLevel2();
    this.getOrgLevel3();
    this.getSubject();
    this.getList();
  },
  computed: {
    checkLevel() {
      return this.$store.state.checkLevel;
    },
    platformInformation() {
      return this.$store.state.platformInformation;
    },
  },
  methods: {
    /**
     * 根据学段获取年级
     */
    getGrade(val) {
      this.listParam.grade_id = null;
      this.listParam.subject_id = null;
      this.orgLevel3 = [{ id: null, name: "全部年级" }];
      this.orgSubject = [{ id: null, name: "全部科目" }];
      this.orgLevel3Param.pid = val;
      this.subjectParam.org_id = val;

      this.getOrgLevel3();
      this.getSubject();
    },
    /**
     * 根据年级获取科目
     */
    getSubjectList(val) {
      this.listParam.subject_id = null;
      this.orgSubject = [{ id: null, name: "全部科目" }];
      this.subjectParam.org_id = val;

      this.getSubject();
    },
    searchFunc() {
      this.listParam.page_index = 1;
      this.getList();
    },
    reset() {
      this.orgLevel2Param = {
        //获取学段参数
        level: 2,
        pid: null,
        direction: "balance",
      };
      this.orgLevel3Param = {
        //获取年级参数
        level: 3,
        pid: null,
        direction: "balance",
      };
      this.subjectParam = {
        //获取科目,版本，参数
        type: 2,
        org_id: null,
        status: 1,
      };
      this.listParam = {
        study_section_id: null,
        grade_id: null,
        subject_id: null,
        keyword: "",
        page_index: 1,
        page_size: 10,
      };
      this.getList();
    },
    // 验证
    validateContent(rule, value, callback) {
      if (value === "") {
        return callback(new Error("请输入拒绝理由"));
      } else if (!validateName(value)) {
        return callback(new Error("不能输入空格符号"));
      } else {
        callback();
      }
    },
    management(id) {
      this.$router.push("/teaching/group/group-management/" + id);
    },
    review(id) {
      if (this.platformInformation) {
        //教育局
        this.$router.push("/teaching/group/member-check/" + id);
      } else {
        //学校
        this.$router.push("/teaching/group/member-review/" + id);
      }
    },
    newGroup(id) {
      this.$router.push("/teaching/group/new-group");
    },
    // 序号
    typeIndex(index) {
      return (
        index + (this.listParam.page_index - 1) * this.listParam.page_size + 1
      );
    },
    getStatus(val) {
      return this.statusArr[parseInt(val)];
    },
    getList() {
      this.loading.status = true;
      let success = (data) => {
        this.loading.status = false;
        this.listData = data.data.data;
        console.log(this.listData);
        this.total_number = data.data.data_sum;
      };
      let fail = (data) => {
        this.loading.status = false;
        this.$message.error(data.msg);
      };
      //执行请求，并将回调方法变成config成员以便鉴权过期可以执行
      this.$ajax.get("api/group/manage_group_list", {
        params: this.listParam,
        func: { success: success, fail: fail },
      });
    },
    //获取学段
		getOrgLevel2(){
			let success = ( data=>{
							this.orgLevel2 = data.data;
							this.orgLevel2.unshift({id:null,name:'全部学段'})
						})
			if (this.$store.state.checkLevel) {
				this.$ajax.get("api/newlevel/platform_org", {
					params: this.orgLevel2Param,
					func: { success: success },
				});
			} else {
				this.$ajax.get("api/org/org", {
					params: this.orgLevel2Param,
					func: { success: success },
				});
			}
		},
		//获取年级
		getOrgLevel3(){
			let success = ( data=>{
					this.orgLevel3 = data.data;
					this.orgLevel3.unshift({id:null,name:'全部年级'})
			})
			if (this.$store.state.checkLevel) {
					this.$ajax.get('api/newlevel/platform_org', { params: this.orgLevel3Param, func: { success: success } })
			}else{
					this.$ajax.get('api/org/org',{params: this.orgLevel3Param,func:{success:success}})
			}
		},
		//获取科目
		getSubject(){
			let success = ( data=>{
					this.orgSubject = data.data;
					console.log(this.orgSubject)
					this.orgSubject.unshift({id:null,name:'全部科目'});
			})
			if (this.$store.state.checkLevel) {
					this.$ajax.get('api/newlevel/platform_subject', { params: this.subjectParam, func: { success: success } })
			} else {
						this.$ajax.get('api/cate/subject',{params: this.subjectParam,func:{success:success}})
			}
		},
    handleSizeChange(val) {
      this.listParam.page_index = 1;
      this.listParam.page_size = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listParam.page_index = val;
      this.getList();
    },
    refusal(obj) {
      // this.approveParam = {
      // 		id:obj.id,
      // 		status:3,
      // 		comment:'',
      // 	}
      // this.refusalVisible = true

      let yes = (data) => {
        this.approveParam = {
          approve_group: 1,
          id: obj.id,
          status: 3,
          comment: "",
        };
        this.approve();
      };
      let cancel = (data) => {};
      this.e_confirm({
        yes: yes,
        cancel: cancel,
        txt: "确定拒绝 " + obj.name + " 吗？",
      });
    },
    closesal(obj) {
      let yes = (data) => {
        this.approveParam = {
          id: obj.id,
          status: 4,
          comment: "",
        };
        this.approve();
      };
      let cancel = (data) => {};

      this.e_confirm({
        yes: yes,
        cancel: cancel,
        txt: "确定关闭 " + obj.name + " 吗？",
      });
    },
    open_confirm(obj) {
      let yes = (data) => {
        this.approveParam = {
          id: obj.id,
          status: 1,
          comment: "",
        };
        this.approve();
      };
      let cancel = (data) => {};
      this.e_confirm({
        yes: yes,
        cancel: cancel,
        txt: "确定开启 " + obj.name + " 吗？",
      });
    },
    approve_confirm(obj) {
      let yes = (data) => {
        this.approveParam = {
          approve_group: 1,
          id: obj.id,
          status: 1,
          comment: "",
        };
        this.approve();
      };
      let cancel = (data) => {};
      this.e_confirm({
        yes: yes,
        cancel: cancel,
        txt: "确定批准 " + obj.name + " 吗？",
      });
    },
    approve(key) {
      let success = (data) => {
        this.refusalVisible = false;
        this.$message.success(data.msg);

        if (key === "del") {
          if (this.listData.length - 1 === 0) {
            if (this.listParam.page_index > 1) {
              this.listParam.page_index--;
            } else {
              this.listParam.page_index = 1;
            }
          }
        }

        this.getList();
      };
      this.$ajax.put("api/group/group_list", this.approveParam, {
        func: { success: success },
      });
    },
    delete_confirm(obj) {
      let yes = (data) => {
        this.approveParam = {
          id: obj.id,
          status: 0,
          comment: "",
        };
        this.approve("del");
      };
      let cancel = (data) => {};
      this.e_confirm({
        yes: yes,
        cancel: cancel,
        txt: "确定将 " + obj.name + " 删除吗？",
      });
    },
    submitForm(formName) {
      // this.$refs[formName].validate((valid) => {
      // 	if (valid) {
      // 		this.approveParam.comment = this.ruleForm.desc;
      // 		this.approve();
      // 	} else {
      // 		return false;
      // 	}
      // });
      this.approve();
    },
  },
};
</script>
<style lang="scss">
.zh-circle-group {
  .el-table__body-wrapper table tbody {
    td:nth-child(13) {
      padding: 0;
    }
  }
}
</style>